package com.wt.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wt.admin.config.SecUtils;
import com.wt.admin.entity.*;
import com.wt.admin.service.WqunSysMenuService;
import com.wt.admin.service.WqunSysRoleService;
import com.wt.commonutils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 账号表 前端控制器
 * </p>
 *
 * @author wt
 * @since 2023-07-14
 */
@Api(tags = "菜单管理模块")
@RestController
@RequestMapping("/sysMenu")
@CrossOrigin
public class WqunSysMenuController {

    @Autowired
    private WqunSysMenuService wqunSysMenuService;

    @Autowired
    private WqunSysRoleService wqunSysRoleService;

    @ApiOperation("添加菜单")
    @PostMapping("/addMenu")
        @PreAuthorize("hasAnyAuthority('super:sysMenu:addMenu')")
    public R addMenu(@RequestBody WqunSysMenu wqunSysMenu){
        QueryWrapper<WqunSysMenu> wqunSysMenuQueryWrapper = new QueryWrapper<>();
        int count = wqunSysMenuService.count(wqunSysMenuQueryWrapper);
        wqunSysMenu.setId((long) (count + 1));
        String id = SecUtils.getUserMess().getId();
        wqunSysMenu.setCreateBy(Long.valueOf(id));
        wqunSysMenu.setUpdateBy(Long.valueOf(id));
//        System.out.println("wqunSysMenu = " + wqunSysMenu);
        boolean b = wqunSysMenuService.save(wqunSysMenu);
        return b?R.ok().message("添加成功"):R.error().message("添加失败");
    }

    @ApiOperation("修改菜单")
    @PostMapping("/updateMenu")
    @PreAuthorize("hasAnyAuthority('super:sysMenu:updateMenu')")
    public R updateMenu(@RequestBody WqunSysMenu wqunSysMenu){
        boolean b = wqunSysMenuService.updateById(wqunSysMenu);
        return b?R.ok().message("修改成功"):R.error().message("修改失败");
    }

    //查询所有菜单
    @ApiOperation("查询所有菜单")
    @GetMapping("/findAllMenu/{roleId}")
    @PreAuthorize("hasAnyAuthority('admin:sysMenu:findAllMenu')")
    public R findAllMenu(@PathVariable("roleId") String roleId){
        QueryWrapper queryWrapper = new QueryWrapper();
//        queryWrapper.orderByDesc("id");
        queryWrapper.select("id","menu_name","remark");
        List<WqunSysMenu> findAllMenu = wqunSysMenuService.list(queryWrapper);
        List<WqunSysMenu> wqunSysRoleMenus = wqunSysRoleService.getRoleMenu(roleId);
//        System.out.println("wqunSysRoleMenus = " + wqunSysRoleMenus);
        return R.ok().data("findAllMenu",findAllMenu).data("wqunSysRoleMenus",wqunSysRoleMenus);
    }



//    //管理员查询所有菜单
//    @ApiOperation("管理员查询所有菜单")
//    @GetMapping("/userFindAllMenu")
//
//    public R userFindAllMenu(){
//        QueryWrapper queryWrapper = new QueryWrapper();
//
//        List<WqunSysMenu> findAllMenu = wqunSysMenuService.list(queryWrapper);
//        return R.ok().data("findAllMenu",findAllMenu);
//    }

    //分页查询所有菜单
    @ApiOperation("分页查询所有菜单")
    @GetMapping("/findMenuPage/{pageSize}/{currentPage}/{selectFind}/{selectFindValue}")
    @PreAuthorize("hasAnyAuthority('admin:sysMenu:findMenuPage')")
    public R findMenuPage(@PathVariable int pageSize,@PathVariable int currentPage,@PathVariable String selectFind,@PathVariable String selectFindValue){
        return wqunSysMenuService.findMenuPage(pageSize,currentPage,selectFind,selectFindValue);
    }


    //禁用菜单
    @ApiOperation("禁用菜单")
    @GetMapping("/bitMenu/{id}")
    @PreAuthorize("hasAnyAuthority('admin:sysMenu:bitMenu')")
    public R bitMenu(@PathVariable String id){
        WqunSysMenu byId = wqunSysMenuService.getById(id);
        byId.setStatus(byId.getStatus().equals("0")?"1":"0");
        boolean b = wqunSysMenuService.updateById(byId);
        return b?R.ok().message("修改成功"):R.error().message("修改失败");
    }

}
